﻿using SharPy.GameClasses;
using SharPy.Interface;
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;

namespace SharPy.Parser.clCommands
{
    class ShowImage : clCommand
    {
         public static bool RegExpCheck(string s)
        {
             if(Regex.IsMatch(s, "^show(.*)", RegexOptions.IgnoreCase))
                    return true;
             else
                 return false;
        }

        public static clCommand Parse(string s)
        {
            if(Regex.IsMatch(s, "^show(.*)with(.*)", RegexOptions.IgnoreCase))
            {
                MatchCollection m = Regex.Matches(s, @"show(.*)with(.*)", RegexOptions.IgnoreCase);
                return (new clCommand(clCommand.Name.ShowImage
                                        , m[0].Groups[1].Value.Trim()
                                        , null
                                        , m[0].Groups[1].Value.Trim()
                        ));
            }
            else if (Regex.IsMatch(s, "^show(.*)at(.*)", RegexOptions.IgnoreCase))
            {
                MatchCollection m = Regex.Matches(s, @"show(.*)at(.*)", RegexOptions.IgnoreCase);
                return (new clCommand(clCommand.Name.ShowImage
                                        , m[0].Groups[1].Value.Trim()
                                        , m[0].Groups[2].Value.Trim()
                        ));
            }
            else if (Regex.IsMatch(s, "^show(.*)", RegexOptions.IgnoreCase))
            {
                MatchCollection m = Regex.Matches(s, @"show(.*)", RegexOptions.IgnoreCase);
                return (new clCommand(clCommand.Name.ShowImage
                                        , m[0].Groups[1].Value.Trim()
                                        , null
                        ));
            }
            else throw new ArgumentException();
        }

        public static async Task<int> CommandHandle(clCommand objCom, GamePage page)
        {
            Grid gridMain = (Grid)page.FindName("gridMain");
            Image imChar = new Image();
            imChar.Source = await clImages.getImage(objCom.sParameter[0]);

            gridMain.Children.Add(imChar);
            imChar.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Bottom;

            ///Implement horizontal positioning
            if (objCom.sParameter[1] == null)
            {
                imChar.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;
                imChar.Visibility = Windows.UI.Xaml.Visibility.Visible;
                clImages.DeleteOldImages(page, Windows.UI.Xaml.HorizontalAlignment.Center, imChar);
                imChar.Name = objCom.sParameter[0];
            }
            else
            {
                throw new NotImplementedException();
            }

            //Add animations
            if(objCom.sParameter[2] != null)
            {
                clAnimations.FadeIn(page, imChar, .2);
            }
            return 0;
        }
    }
}
